﻿#include "pch.h"
#include <iostream>
#include <string>
#include<Windows.h>
#include "bookMgr.h"
#include "studentMgr.h"
#include "ClerkMgr.h"

int login();
void manager_manage();
void student_manage();
void book_manage();
void borrow_manage();
Book input_info();
Clerk input_clerk();

using namespace std;

//输入图书信息
Book input_info()
{
	//姓名 书号  入库年 入库月 入库日 借阅年(0表示无借阅) 借阅月 借阅日 借阅者姓名(n表示无借阅者) 在库状态（true：在库，false：借出）
	Book book{};
	cout << "请输入书名：";
	cin >> book.name;
	cout << endl;
	cout << "请输入书编号：";
	cin >> book.bookSn;
	cout << endl;
	cout << "请输入入库年(xxxx)：";
	cin >> book.stDt.y;
	cout << endl;
	cout << "请输入入库月(1-12)：";
	cin >> book.stDt.m;
	cout << endl;
	cout << "请输入入库日(1-31)：";
	cin >> book.stDt.d;
	cout << endl;
	cout << "请输入借阅年(****)（入库输入0）：";
	cin >> book.boDt.y;
	cout << endl;
	cout << "请输入借阅月(1-12)（入库输入0）：";
	cin >> book.boDt.m;
	cout << endl;
	cout << "请输入借阅日(1-31)（入库输入0）：";
	cin >> book.boDt.d;
	cout << endl;
	cout << "请输入借阅者学号（入库输入0）：";
	cin >> book.borrower;
	cout << endl;
	cout << "请输入在库状态（入库输入1）：";
	cin >> book.flag;
	cout << endl;
	return book;
}

Clerk input_clerk()
{
	Clerk clerk{};
	// 账户，姓名，性别，年龄，密码，出生年 出生月 出生日
	cout << "请输入新添加管理员账号：";
	cin >> clerk.account;
	cout << endl;
	cout << "请输入新添加管理员姓名：";
	cin >> clerk.name;
	cout << endl;
	cout << "请输入新添加管理员性别（1：男，0：女）：";
	cin >> clerk.sex;
	cout << endl;
	cout << "请输入新添加管理员年龄：";
	cin >> clerk.age;
	cout << endl;
	cout << "请输入新添加管理员密码：";
	cin >> clerk.password;
	cout << endl;
	cout << "请输入新添加管理员出生年：";
	cin >> clerk.birth.y;
	cout << endl;
	cout << "请输入新添加管理员出生月：：";
	cin >> clerk.birth.m;
	cout << endl;
	cout << "请输入新添加管理员出生日：";
	cin >> clerk.birth.d;
	cout << endl;
	return clerk;
}

// 登录管理
/*
int login()
{
	string account, passwd;
	cout << "请输入管理员账号(输入0则退出程序)：";
	cin >> account;
	if (account == "0")
	{
		return 0;
	}
	cout << endl;
	cout << "请输入密码：";
	cin >> passwd;
	cout << endl;
	if (account == "zmy")
	{
		if (passwd == "12345")
		{
			return 1;
		}

	}
	return 2;
}
*/

// 管理员管理
void manager_manage()
{
	Clerk clerk{};
	ClerkMgr cmgr{};
	string clerkaccount{ "" };
	bool del_c{ false };
	char choice = '1';
	while (choice != '0')
	{
		system("cls");
		cout << "\t----------图书管理系统---------\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "\t1    管理员增加\t" << endl;
		cout << "\t2    管理员删除\t" << endl;
		cout << "\t3    管理员修改\t" << endl;
		cout << "\t4    管理员查询\t" << endl;
		cout << "\t0    退出\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "请选择需要进行的操作序号：";
		cin >> choice;
		switch (choice)
		{
		case '0':
			system("cls");
			break;
		case '1':
			system("cls");
			clerk = input_clerk();
			cmgr.addClerk(clerk);
			cout << "存储成功！(3秒后自动退出)" << endl;
			Sleep(3000);
			break;
		case '2':
			system("cls");
			cout << "请输入要删除的管理员账号：";
			cin >> clerkaccount;
			cout << endl;
			del_c = cmgr.delClerk(clerkaccount);
			if (del_c)
			{
				cout << "删除成功！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		case '3':
			system("cls");
			cout << "输入要修改的管理员账号:" << endl;
			cin >> clerkaccount;
			cout << "--输入要修改的管理员信息--" << endl;
			clerk = input_clerk();
			cmgr.remendClerk(clerk, clerkaccount);
			cout << "修改成功！(3秒后自动退出)" << endl;
			Sleep(3000);
			break;
		case '4':
			system("cls");
			cout << "输入要查询的管理员账号：";
			cin >> clerkaccount;
			clerk = cmgr.queryClerk(clerkaccount);
			if (clerk.name != "")
			{
				cout << clerk.account << " " << clerk.name << " " << clerk.sex << " " << clerk.age << " " << clerk.password
					<< " " << clerk.birth.y << " " << clerk.birth.m << " " << clerk.birth.d << endl;
			}
			else
			{
				cout << "查无此人！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		default:
			break;
		}
	}
}

// 学生管理
void student_manage()
{
	Student stu{};
	studentMgr sMgr{};
	string studentid{ "" };
	string studentname{ "" };
	bool del_s{ false };
	char choice = '1';
	while (choice != '0')
	{
		system("cls");
		cout << "\t----------图书管理系统---------\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "\t1    学生增加\t" << endl;
		cout << "\t2    学生删除\t" << endl;
		cout << "\t3    学生修改\t" << endl;
		cout << "\t4    学生查询\t" << endl;
		cout << "\t0    退出\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "请选择需要进行的操作序号：";
		cin >> choice;
		switch (choice)
		{
		case '0':
			system("cls");
			break;
		case '1':
			system("cls");
			cout << "请输入新添加学生学号：";
			cin >> stu.studentId;
			cout << endl;
			cout << "请输入新添加学生姓名：";
			cin >> stu.name;
			cout << endl;
			sMgr.addStudent(stu);
			cout << "存储成功！" << endl;
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		case '2':
			system("cls");
			cout << "请输入要删除的学生学号：";
			cin >> studentid;
			del_s = sMgr.delStudent(studentid);
			if (del_s)
			{
				cout << "删除成功！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		case '3':
			system("cls");
			cout << "输入要修改的学生学号:" << endl;
			cin >> studentid;
			cout << "--输入要修改的学生信息--" << endl;
			cout << "请输入新添加学生学号：";
			cin >> stu.studentId;
			cout << endl;
			cout << "请输入新添加学生姓名：";
			cin >> stu.name;
			cout << endl;
			sMgr.remendStudent(stu, studentid);
			cout << "修改成功！(3秒后自动退出)" << endl;
			Sleep(3000);
			break;
		case '4':
			system("cls");
			cout << "输入要查询的学生姓名：";
			cin >> studentname;
			stu = sMgr.queryStudent(studentname);
			if (stu.name != "")
			{
				cout << stu.studentId << " " << stu.name<< endl;
			}
			else
			{
				cout << "查无此人！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		default:
			break;
		}
	}
}

// 图书管理
void book_manage()
{
	Book book{};
	bookMgr bmgr{};
	string booksn;
	string name;
	bool del_b;
	char choice = '1';
	while (choice != '0')
	{
		system("cls");
		cout << "\t----------图书管理系统---------\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "\t1    图书增加\t" << endl;
		cout << "\t2    图书删除\t" << endl;
		cout << "\t3    图书修改\t" << endl;
		cout << "\t4    图书查询\t" << endl;
		cout << "\t0    退出\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "请选择需要进行的操作序号：";
		cin >> choice;
		switch (choice)
		{
		case '0':
			system("cls");
			break;
		case '1':
			system("cls");
			bmgr.addBook(input_info());
			cout << "存储成功！(3秒后自动退出)" << endl;
			Sleep(3000);
			break;
		case '2':
			system("cls");
			cout << "请输入要删除的图书书号：";
			cin >> booksn;
			del_b = bmgr.delBook(booksn);
			if (del_b)
			{
				cout << "删除成功！(3秒后自动退出)" << endl;
			}
			Sleep(3000);
			break;
		case '3':
			system("cls");
			cout << "输入要修改的图书书号:" << endl;
			cin >> booksn;
			cout << "--输入要修改的图书信息--" << endl;
			book = input_info();
			bmgr.remendBook(book, booksn);
			cout << "修改成功！(3秒后自动退出)" << endl;
			Sleep(3000);
			break;
		case '4':
			system("cls");
			cout << "输入要查询的书名：";
			cin >> name;
			book = bmgr.queryBook(name);
			if (book.stDt.y !=0)
			{
				cout << book.name << " " << book.bookSn << " " << book.stDt.y << " " << book.stDt.m << " " << book.stDt.d << " "
					<< book.boDt.y << " " << book.boDt.m << " " << book.boDt.d << " " << book.borrower << " " << book.flag << endl;
			}
			else
			{
				cout << "书库中没有这本书！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		default:
			cout << "输入错误，请重新输入！" << endl;
			Sleep(3000);
			break;
		}
	}
}

// 借阅信息管理
void borrow_manage()
{
	Book book{};
	bookMgr bmgr{};
	studentMgr smgr{};
	string bookSn{};
	string stuID{};
	vector<Book> borrower_info{};
	char choice = '1';
	while (choice != '0')
	{
		system("cls");
		cout << "\t----------图书管理系统---------\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "\t1    借书\t" << endl;
		cout << "\t2    还书\t" << endl;
		cout << "\t3    借阅信息查询\t" << endl;
		cout << "\t0    退出\t" << endl;
		cout << "\t******************************\t" << endl;
		cout << "请选择需要进行的操作序号：";
		cin >> choice;
		switch (choice)
		{
		case '0':
			system("cls");
			break;
		case '1':
			system("cls");
			// 检查存在该学生
			cout << "请输入借阅者学号：";
			cin >> stuID;
			cout << endl;
			if (! smgr.checkStudent(stuID))
			{
				cout << "查不到该学生信息！" << endl;
				cout << "3秒后自动退出" << endl;
				Sleep(3000);
				break;
			}
			cout << "输入要查询的书号：";
			cin >> bookSn;
			book = bmgr.selectBook(bookSn);
			//证明存在这本书
			if (book.stDt.y != 0)
			{
				if (book.flag)
				{
					cout << "请输入借阅年：";
					cin >> book.boDt.y;
					cout << endl;
					cout << "请输入借阅月：";
					cin >> book.boDt.m;
					cout << endl;
					cout << "请输入借阅日：";
					cin >> book.boDt.d;
					cout << endl;
					book.borrower = stuID;
					book.flag = 0;
					bmgr.remendBook(book, bookSn);
					cout << "借书信息已存入！" << endl;
				}
				else
				{
					cout << "该书已被借出！" << endl;
				}
			}
			else
			{
				cout << "书库中没有这本书！" << endl;
			}
			cout << "3秒后自动退出" << endl;
			Sleep(3000);
			break;
		case '2':
			system("cls");
			// 还书肯定书库存在这本书
			cout << "输入要归还图书的书号：";
			cin >> bookSn;
			book = bmgr.selectBook(bookSn);
			//证明存在这本书
			if (book.stDt.y != 0)
			{

				book.boDt.y=0;
				book.boDt.m=0;
				book.boDt.d=0;
				book.borrower="0";
				book.flag = 1;
				bmgr.remendBook(book, bookSn);
				cout << "还书信息已修正！" << endl;
			}
			else
			{
				cout << "书库中没有这本书！" << endl;
			}
			cout << "3秒后自动退出！" << endl;
			Sleep(3000);
			break;
		case '3':
			system("cls");
			cout << "请输入查询学生学号：";
			cin >> stuID;
			cout << endl;
			borrower_info = bmgr.checkbook_by_stuID(stuID);
			if (borrower_info.size() != 0)
			{
				cout << "学生：" << stuID << " 借阅书籍如下：" << endl;
				for (auto b : borrower_info)
				{
					cout << b.bookSn << "      " << b.name << endl;
				}
			}
			else
			{
				cout << "该学生没有借阅记录！" << endl;
			}
			cout << endl;
			cout << endl;
			cout << "输入任意字符退出！" << endl;
			system("PAUSE");
			break;
		default:
			break;
		}
	}
}


int main()
{
	ClerkMgr cmg{};
	int come = cmg.login();
	while (come !=0)//程序一直运行
	{
		if (come==2)
		{
			cout << "用户名/密码输入错误！" << endl;
			come = cmg.login();
		}
		else if(come==1)
		{
			system("cls");
			char choice = '1';
			while (choice != '0')
			{
				cout << "\t----------图书管理系统---------\t" << endl;
				cout << "\t******************************\t" << endl;
				cout << "\t1    管理员信息管理\t" << endl;
				cout << "\t2    学生信息管理\t" << endl;
				cout << "\t3    借阅信息管理\t" << endl;
				cout << "\t4    图书信息管理\t" << endl;
				cout << "\t0    退出\t" << endl;
				cout << "\t******************************\t" << endl;

				cout << "请选择需要进行的操作序号：";
				cin >> choice;            //接收用户输入的选项
				switch (choice)
				{
				case '1':
					system("cls");
					manager_manage();
					break;
				case '2':
					system("cls");
					student_manage();
					break;
				case '3':
					system("cls");
					borrow_manage();
					break;
				case '4':
					system("cls");
					book_manage();
					break;
				case '0':
					come = 0;
					break;
				default:
					cout << "输入错误，请重新输入！" << endl;
					break;
				}
			}
		}
	}
	return 0;
}
